home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / WFC010.ZIP / SRC / LZFILE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  4.4 KB  |  207 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like as long as you don't try to sell it.
  10. **
  11. ** Any attempt to sell WFC in source code form must have the permission
  12. ** of the original author. You can produce commercial executables with
  13. ** WFC but you can't sell WFC.
  14. **
  15. ** Copyright, 1995, Samuel R. Blackburn
  16. **
  17. ** $Workfile: $
  18. ** $Revision: $
  19. ** $Modtime: $
  20. */
  21.  
  22. #if defined( _DEBUG )
  23. #undef THIS_FILE
  24. static char BASED_CODE THIS_FILE[] = __FILE__;
  25. #define new DEBUG_NEW
  26. #endif
  27.  
  28. CLZFile::CLZFile()
  29. {
  30.    m_Initialize();
  31. }
  32.  
  33. CLZFile::~CLZFile()
  34. {
  35.    TRACE( "Destroying a CLZFile object\n" );
  36.    Close();
  37. }
  38.  
  39. void CLZFile::Close( void )
  40. {
  41.    ASSERT_VALID( this );
  42.  
  43.    if ( m_LZFileHandle >= 0 )
  44.    {
  45.       ::LZClose( m_LZFileHandle );
  46.       m_LZFileHandle = LZERROR_BADINHANDLE;
  47.       m_hFile = CFile::hFileNull;
  48.    }
  49.  
  50.    m_Initialize();
  51. }
  52.  
  53. BOOL CLZFile::Copy( const CLZFile * source_p )
  54. {
  55.    return( Copy( *source_p ) );
  56. }
  57.  
  58. BOOL CLZFile::Copy( const CLZFile& source )
  59. {
  60.    LONG size_of_destination_file = 0;
  61.  
  62.    size_of_destination_file = ::LZCopy( source.m_LZFileHandle, m_LZFileHandle );
  63.  
  64.    if ( size_of_destination_file < 0 )
  65.    {
  66.       m_ErrorCode = size_of_destination_file;
  67.       return( FALSE );
  68.    }
  69.  
  70.    return( TRUE );
  71. }
  72.  
  73. #if defined( _DEBUG )
  74.  
  75. void CLZFile::Dump( CDumpContext& dump_context ) const
  76. {
  77.    CDummyFile::Dump( dump_context );
  78.  
  79.    dump_context << "m_LZFileHandle = " << m_LZFileHandle << "\n";
  80.    dump_context << "m_OpenFileStructure is ";
  81.    m_OpenFileStructure.Dump( dump_context );
  82. }
  83.  
  84. #endif // _DEBUG
  85.  
  86. BOOL CLZFile::GetExpandedName( LPTSTR name_of_compressed_file, CString& original_file_name )
  87. {
  88.    TCHAR file_name[ _MAX_PATH ];
  89.  
  90.    ::ZeroMemory( file_name, sizeof( file_name ) );
  91.  
  92.    if ( ::GetExpandedName( name_of_compressed_file, file_name ) == 1 )
  93.    {
  94.       original_file_name = file_name;
  95.       return( TRUE );
  96.    }
  97.    else
  98.    {
  99.       original_file_name.Empty();
  100.       return( FALSE );
  101.    }
  102. }
  103.  
  104. void CLZFile::m_Initialize( void )
  105. {
  106.    m_LZFileHandle = 0;
  107.    m_OpenFileStructure.Empty();
  108. }
  109.  
  110. BOOL CLZFile::Open( const char *file_name, UINT style, CFileException * error_p )
  111. {
  112.    m_LZFileHandle = ::LZOpenFile( (char *) file_name, &m_OpenFileStructure, style );
  113.  
  114.    if ( m_LZFileHandle < 0 )
  115.    {
  116.       m_ErrorCode = m_LZFileHandle;
  117.  
  118.       return( FALSE );
  119.    }
  120.  
  121.    return( TRUE );
  122. }
  123.  
  124. UINT CLZFile::Read( void *buffer, UINT size_of_buffer )
  125. {
  126.    INT number_of_bytes_read = 0;
  127.  
  128.    number_of_bytes_read = ::LZRead( m_LZFileHandle, (char *) buffer, size_of_buffer );
  129.  
  130.    if ( number_of_bytes_read < 0 )
  131.    {
  132.       m_ErrorCode = number_of_bytes_read;
  133.       return( 0 );
  134.    }
  135.  
  136.    return( (UINT) number_of_bytes_read );
  137. }
  138.  
  139. LONG CLZFile::Seek( LONG offset, UINT from )
  140. {
  141.     ASSERT_VALID( this );
  142.     ASSERT( m_hFile != (UINT) CFile::hFileNull );
  143.     ASSERT( from == begin || from == end || from == current);
  144.     ASSERT( begin == FILE_BEGIN && end == FILE_END && current == FILE_CURRENT );
  145.  
  146.     LONG offset_from_beginning_of_file = ::LZSeek( m_LZFileHandle, offset, from );
  147.  
  148.    if ( offset_from_beginning_of_file < 0 )
  149.    {
  150.       m_ErrorCode = offset_from_beginning_of_file;
  151.    }
  152.  
  153.     return( offset_from_beginning_of_file );
  154. }
  155.  
  156. void CLZFile::TranslateErrorCode( int error_code, CString& error_message )
  157. {
  158.    switch( error_code )
  159.    {
  160.       case LZERROR_BADINHANDLE:
  161.  
  162.          error_message = "Invalid input handle";
  163.          return;
  164.  
  165.       case LZERROR_BADOUTHANDLE:
  166.  
  167.          error_message = "Invalid output handle";
  168.          return;
  169.  
  170.       case LZERROR_READ:
  171.  
  172.          error_message = "Corrupt compressed file format";
  173.          return;
  174.  
  175.       case LZERROR_WRITE:
  176.  
  177.          error_message = "Out of space for output file";
  178.          return;
  179.  
  180.       case LZERROR_GLOBALLOC:
  181.  
  182.          error_message = "Insufficient memory for LZFile struct";
  183.          return;
  184.  
  185.       case LZERROR_GLOBLOCK:
  186.  
  187.          error_message = "Bad global handle";
  188.          return;
  189.  
  190.       case LZERROR_BADVALUE:
  191.  
  192.          error_message = "Input parameter out of acceptable range";
  193.          return;
  194.  
  195.       case LZERROR_UNKNOWNALG:
  196.  
  197.          error_message = "Compression algorithm not recognized";
  198.          return;
  199.  
  200.       default:
  201.  
  202.          error_message.Format( "Unknown error number %d", error_code );
  203.          return;
  204.    }
  205. }
  206.  
  207.